//
//  JyInformationCollectionTableCell.m
//  WhereToTravel
//
//  Created by dlios on 15/10/15.
//  Copyright © 2015年 zhoushengmin. All rights reserved.
//

#import "JyInformationCollectionTableCell.h"
#import "JyInCollectionViewCell.h"
#import "WonderfulModel.h"

@implementation JyInformationCollectionTableCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(WIDTH *185, HEIGHT * 80);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 20 * HEIGHT;
    
    self.journeyCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH * 375, HEIGHT * 360) collectionViewLayout:flowLayout];
    self.journeyCollectionView.backgroundColor = [UIColor clearColor];
    self.journeyCollectionView.scrollEnabled = NO;
    self.journeyCollectionView.frame = CGRectZero;
    self.journeyCollectionView.dataSource = self;
    self.journeyCollectionView.delegate = self;
    [self.contentView addSubview:self.journeyCollectionView];
    
    [self.journeyCollectionView registerClass:[JyInCollectionViewCell class] forCellWithReuseIdentifier:@"reuseOne"];
  
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arr.count;
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    JyInCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuseOne" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    
    // 数据解析
    self.modelArr = [NSMutableArray array];
    for (NSMutableDictionary *dic in self.arr) {
        WonderfulModel *wonderfulModel = [[WonderfulModel alloc] init];
        [wonderfulModel setValuesForKeysWithDictionary:dic];
        [self.modelArr addObject:wonderfulModel];
    }

    
    
    
    WonderfulModel *wonderfulModel = self.modelArr[indexPath.row];
        
    [cell.upImageView sd_setImageWithURL:[NSURL URLWithString:wonderfulModel.photo] placeholderImage:[UIImage imageNamed:@"占位图"]];
    cell.mistLabel.text = wonderfulModel.title;
    cell.downLeftLabel.text = wonderfulModel.priceoff;
    NSString *strString = wonderfulModel.price;
    if (strString.length == 15) {
        NSString *str = [strString substringWithRange:NSMakeRange(4, 4)];
        str = [NSString stringWithFormat:@"%@元起",str];
        cell.downRightLabel.text = str;
    }
    else if (strString.length == 16){
        
        NSString *str = [strString substringWithRange:NSMakeRange(4, 5)];
        str = [NSString stringWithFormat:@"%@元起",str];
        cell.downRightLabel.text = str;
    }
    else if (strString.length == 14) {
        NSString *str = [strString substringWithRange:NSMakeRange(4, 3)];
        str = [NSString stringWithFormat:@"%@元起",str];
        cell.downRightLabel.text = str;
    }

    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    // 数据解析
    self.modelArr = [NSMutableArray array];
    for (NSMutableDictionary *dic in self.arr) {
        WonderfulModel *wonderfulModel = [[WonderfulModel alloc] init];
        [wonderfulModel setValuesForKeysWithDictionary:dic];
        [self.modelArr addObject:wonderfulModel];
    }

    
    WonderfulModel *wonderfulModel = self.modelArr[indexPath.row];
    NSString *strId = [NSString stringWithFormat:@"%@",wonderfulModel.mId];
    self.block(strId);
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.journeyCollectionView.frame = CGRectMake(0, 0, WIDTH * 375, HEIGHT * 360);
}






@end
